package com.lijian.service.impl;

import com.lijian.base.CodeMsg;
import com.lijian.base.Result;
import com.lijian.dto.RoleDto;
import com.lijian.mapper.RoleMapper;
import com.lijian.mapper.RolePermissionMapper;
import com.lijian.mapper.RoleUserMapper;
import com.lijian.model.SysRole;
import com.lijian.model.SysRoleUser;
import com.lijian.model.SysUser;
import com.lijian.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private RoleUserMapper roleUserMapper;

    @Autowired
    private RolePermissionMapper rolePermissionMapper;

    @Override
    public Result<SysRole> findAllRoles() {
        List<SysRole> roles = roleMapper.findAllRole();
        Long count = roleMapper.countAllRoles();
        return Result.success(count.intValue(), roles);
    }

    @Override
    public Result<SysRole> findAllRolesByPage(Integer offset, Integer limit) {
        return Result.success(roleMapper.countAllRoles().intValue(), roleMapper.findAllRolesByPage(offset, limit));
    }

    @Override
    public Result<SysRole> save(RoleDto roleDto) {

        //角色名不能重复
        SysRole dbRole = roleMapper.findRoleByRoleName(roleDto.getName());
        if (dbRole != null) {
            return Result.error(CodeMsg.USERNAME_REPEAT.getCode(), CodeMsg.USERNAME_REPEAT.getMessage());
        }

        //保存角色
        roleDto.setCreateTime(new Date());
        roleDto.setUpdateTime(new Date());
        roleMapper.save(roleDto);
        List<Long> permissionIds = roleDto.getPermissionIds();
        //移除0，permission id是从1开始
        permissionIds.remove(0L);
        //保存角色对应的所有权限
        if(!CollectionUtils.isEmpty(permissionIds)){
            rolePermissionMapper.save(roleDto.getId(), permissionIds);
        }
        return Result.success();
    }

    @Override
    public SysRole findRoleById(Integer id) {
        return roleMapper.findRoleById(id);
    }

    @Transactional
    @Override
    public Result<SysRole> update(RoleDto roleDto) {
        List<Long> permissionIds = roleDto.getPermissionIds();
        permissionIds.remove(0L);
        //更新角色权限之前要删除该角色之前的所有权限
        rolePermissionMapper.deleteRolePermission(roleDto.getId());

        //判断该角色是否有赋予权限制，有就添加
        if(!CollectionUtils.isEmpty(permissionIds)){
            rolePermissionMapper.save(roleDto.getId(),permissionIds);
        }
        roleMapper.update(roleDto);
        return Result.success();
    }

    @Override
    public Result delete(Integer id) {
        List<SysRoleUser> sysRoleUsers = roleUserMapper.findAllSysRoleUserByRoleId(id);
        if(sysRoleUsers.size() <= 0){
            roleMapper.delete(id);
            return Result.success();
        }
        return Result.error(CodeMsg.USER_ROLE_NO_CLEAR.getCode(), CodeMsg.USER_ROLE_NO_CLEAR.getMessage());
    }

    @Override
    public Result<SysRole> findRoleBySearch(String roleName, Integer offset, Integer limit) {
        return Result.success(roleMapper.countRoleBySearch(roleName).intValue(), roleMapper.findRoleBySearch(roleName, offset, limit));
    }
}
